package com.help.help.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.help.help.entity.UserEntity;
import com.help.help.service.UserService;
import io.netty.util.internal.StringUtil;
import okhttp3.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.help.help.dao.AddressDao;
import com.help.help.entity.AddressEntity;
import com.help.help.service.AddressService;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {
 @Autowired
    private UserService userService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
         QueryWrapper<AddressEntity> wrapper=new QueryWrapper<>();
        QueryWrapper<UserEntity> userWrapper=new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtil.isNullOrEmpty(key)){
             userWrapper.like("name",key);
            List<UserEntity> list = userService.list(userWrapper);
            if (list.size()>0) {
                for (int i = 0; i < list.size(); i++) {
                    wrapper.eq("user_id", list.get(i).getId());
                    if (i!=list.size()-1){
                       wrapper.or();
                    }
                }
            } else {
                wrapper.like("address", key);
            }
        }

        IPage<AddressEntity> page = this.page(
                new Query<AddressEntity>().getPage(params),wrapper
        );
        List<AddressEntity> records = page.getRecords();
        for (AddressEntity record : records) {
            if(record.getUserId()!=null){
                record.setUserEntity(userService.getById(record.getUserId()));
            }
        }
        return new PageUtils(page);
    }

}